package com.zyt.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyt.mapper.ClazzMapper;
import com.zyt.mapper.EmpExprMapper;
import com.zyt.mapper.EmpMapper;
import com.zyt.mapper.OperateLogMapper;
import com.zyt.pojo.*;
import com.zyt.service.EmpLogService;
import com.zyt.service.EmpService;
import com.zyt.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Override
    public LoginInfo login(Emp emp) {
        //调用mapper接口,根据用户名和密码查询员工信息
        Emp e = empMapper.selectByUsernameAndPassword(emp);
        //判断用户名和密码是否正确
        if (e != null) {
            System.out.println("登录成功,员工信息为:" + e);
            //生成JWT令牌
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("username", e.getUsername());
            String token = JwtUtils.generateToken(claims);
            return new LoginInfo(e.getId(), e.getUsername(), e.getName(), token);
        }
        //返回登录信息
        return null;
    }

    @Override
    public PageResult <OperateLog> selectLog(LogParam logParam) {
        //1.设置分页参数
       PageHelper.startPage(logParam.getPage(), logParam.getPageSize());
        //2.执行查询
       List<OperateLog> logPage =empMapper.selectLogPage();
       //3,解析查询结果并封装
       Page<OperateLog> p = (Page<OperateLog>) logPage;
       return new PageResult<OperateLog>(p.getTotal(), p.getResult());


    }

    @Override
    public List<Emp> findAll() {
        return empMapper.findAll();
    }

    @Autowired
    private ClazzMapper clazzMapper;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void update(Emp emp) {
        //1.根据Id修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateByid(emp);
        //2,根据ID修改员工的工作经历
        // 1.先删除
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //2.在修改
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }


    }

    @Override
    public Emp getinfo(Integer id) {
        return empMapper.getByid(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        // 批量删除员工基本信息
        empMapper.deleteByIds(ids);
        //批量删除员工工作经历
        empExprMapper.deleteByEmpIds(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Emp emp) {
        //保存员工基本信息
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

            //批量保存员工的工作经验
            Integer empId = emp.getId();
            List<EmpExpr> exprList = emp.getExprList();
            if (!CollectionUtils.isEmpty(exprList)) {
                exprList.forEach(empExpr -> empExpr.setEmpId(empId));
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            //记录操作日志
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "保存员工信息" + emp);
            empLogService.insertLog(empLog);
        }

    }

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
    @Autowired
    private OperateLogMapper operateLogMapper;

    /*
    原始分页查询
     */
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
//        //调用mapper接口获取总记录数
//        Long total = empMapper.count();
//        //调用mapper接口获取结果列表
//        Integer start = (page-1)*pageSize;
//        List<Emp> rows = empMapper.list(start, pageSize);
//        //封装PageResult对象并返回
//        return new PageResult<Emp>(total,rows);
//    }
    /*
     * PageHelper 分页查询

     */
//    @Override
//    public PageResult<Emp> page(Integer page, Integer pageSize) {
//        //1,设置分页参数
//        PageHelper.startPage(page, pageSize);
//        //2,执行查询
//        List<Emp> rows = empMapper.list();
//        //3,解析查询结果,并封装
//        Page<Emp> p = (Page<Emp>) rows;//强转
//        return new PageResult<Emp>(p.getTotal(), p.getResult());
//    }
    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //1,设置分页参数
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        //2,执行查询
        List<Emp> emplist = empMapper.list(empQueryParam);
        //3,解析查询结果,并封装
        Page<Emp> p = (Page<Emp>) emplist;//强转
        return new PageResult<Emp>(p.getTotal(), p.getResult());
    }

}
